package neusoft.edu.cn.a215lmeordering.activities;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import neusoft.edu.cn.a215lmeordering.Listener.ListListener;
import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.adapter.ShopAdapter;
import neusoft.edu.cn.a215lmeordering.adapter.ShopFoodAdapter;
import neusoft.edu.cn.a215lmeordering.bean.Food;
import neusoft.edu.cn.a215lmeordering.bean.Shop;
import neusoft.edu.cn.a215lmeordering.iface.OnItemClickListener;
import neusoft.edu.cn.a215lmeordering.model.FoodSearchModel;
import neusoft.edu.cn.a215lmeordering.model.ShopListModel;

public class SearchActivity extends AppCompatActivity {
    private List<Food> List;
    RecyclerView recyclerView;
    ShopFoodAdapter viewAdapter;

    private EditText textView;
    private Button search;


    ListListener shopListListener = new ListListener() {
        @Override
        public void onResponse(List list) {
            List = list;
            viewAdapter.setDate(List);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };

    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getApplicationContext(), FoodDetailsActivity.class);
            intent.putExtra("food_id",List.get(position).getFood_id());
            startActivity(intent);
            finish();

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_search);

        viewAdapter = new ShopFoodAdapter();
        viewAdapter.setOnItemClickListener(itemClickListener);

        recyclerView = findViewById(R.id.search_recycler);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);

//        //添加自己设计的分割线
//        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL);
//        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(getApplicationContext(),R.drawable.divider));
//        recyclerView.addItemDecoration(dividerItemDecoration);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(viewAdapter);

        textView=findViewById(R.id.food_search_textview);
        search=findViewById(R.id.food_search);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FoodSearchModel foodSearchModel = new FoodSearchModel();
                foodSearchModel.getResultList(textView.getText().toString(),shopListListener);
            }
        });

    }
}
